home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-06-16 | 4.4 KB | 186 lines | [TEXT/MPS ] |
- /*
- File: Utilities.c
-
- Copyright: © 1997-1998 by Apple Computer, Inc., all rights reserved.
-
- */
-
- //
- // You may incorporate this sample code into your applications
- // without restriction. This sample code has been provided "AS
- // IS" and the responsibility for its operation is 100% yours.
- // You are not permitted to redistribute the source as "Apple
- // sample code" after having made changes. If you're going to
- // re-distribute the source, we require that you make it clear
- // in the source that the code was descended from Apple sample
- // code, but that you've made changes.
- //
-
- #pragma segment AppSeg
-
- #ifndef __SCRAP__
- #include <Scrap.h>
- #endif
-
- #ifndef __TEXTEDIT__
- #include <TextEdit.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
-
-
- short myTECut(TEHandle theTE);
- short myTEPaste(TEHandle theTE, short* spaceBefore, short* spaceAfter);
- short TEIsFrontOfLine(short offset, TEHandle theTE);
- short TEGetLine(short offset, TEHandle theTE);
-
-
- // *****************************************************************************
- // *
- // * myTECut()
- // *
- // *****************************************************************************
- short myTECut(TEHandle theTE)
- {
- OffsetTable startOffsets;
- OffsetTable endOffsets;
- short selStart, selEnd, characters;
- Handle theScrap;
-
- if (!(characters = (**theTE).selStart - (**theTE).selEnd))
- return 0;
-
- FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selStart,true,0L,startOffsets);
- FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selEnd,false,0L,endOffsets);
-
- if ((startOffsets[0].offFirst == (**theTE).selStart) &&
- (endOffsets[0].offSecond == (**theTE).selEnd))
- {
- // both the beginning and end of the current selection is on word boundaries
- selStart = (**theTE).selStart;
- selEnd = (**theTE).selEnd;
- if ((*((**theTE).hText))[selStart - 1] == ' ')
- {
- TESetSelect(selStart - 1,selEnd,theTE);
- TECut(theTE);
- theScrap = TEScrapHandle();
- BlockMove((char*)(*theScrap) + 1,(*theScrap),TEGetScrapLength() - 1);
- TESetScrapLength(TEGetScrapLength() - 1);
- ZeroScrap();
- TEToScrap();
- characters--;
- }
- else
- if ((*((**theTE).hText))[selEnd] == ' ')
- {
- TESetSelect(selStart,selEnd + 1,theTE);
- TECut(theTE);
- TESetScrapLength(TEGetScrapLength() - 1);
- ZeroScrap();
- TEToScrap();
- characters--;
- }
- else
- TECut(theTE);
- }
- else
- TECut(theTE);
-
- return characters;
- }
-
-
- // *****************************************************************************
- // *
- // * myTEPaste()
- // *
- // *****************************************************************************
- short myTEPaste(TEHandle theTE, short* spaceBefore, short* spaceAfter)
- {
- OffsetTable startOffsets;
- OffsetTable endOffsets;
- short addSpaceAfter;
- short characters;
-
- characters = (**theTE).selStart - (**theTE).selEnd;
-
- if (spaceBefore)
- *spaceBefore = false;
- if (spaceAfter)
- *spaceAfter = false;
-
- FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selStart,false,0L,startOffsets);
- FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selEnd,true,0L,endOffsets);
-
- addSpaceAfter = ((endOffsets[0].offFirst == (**theTE).selEnd) && ((*((**theTE).hText))[(**theTE).selEnd] != ' '));
-
- if ((startOffsets[0].offSecond == (**theTE).selStart) && ((*((**theTE).hText))[(**theTE).selStart - 1] != ' '))
- {
- TEKey(' ',theTE);
- characters++;
- if (spaceBefore)
- *spaceBefore = true;
- }
-
- TEPaste(theTE);
- characters += TEGetScrapLength();
-
- if (addSpaceAfter)
- {
- TEKey(' ',theTE);
- characters++;
- if (spaceAfter)
- *spaceAfter = true;
- }
-
- return characters;
- }
-
-
- // *****************************************************************************
- // *
- // * TEIsFrontOfLine()
- // *
- // *****************************************************************************
- short TEIsFrontOfLine(short offset, TEHandle theTE)
- {
- short line = 0;
-
- if ((**theTE).teLength == 0)
- return(true);
-
- if (offset >= (**theTE).teLength)
- return ((*((**theTE).hText))[(**theTE).teLength - 1] == 0x0d);
-
- while ((**theTE).lineStarts[line] < offset)
- line++;
-
- return ((**theTE).lineStarts[line] == offset);
- }
-
-
- // *****************************************************************************
- // *
- // * TEGetLine()
- // *
- // *****************************************************************************
- short TEGetLine(short offset, TEHandle theTE)
- {
- short line = 0;
-
- if (offset > (**theTE).teLength)
- return((**theTE).nLines);
-
- while ((**theTE).lineStarts[line] < offset)
- line++;
-
- return line;
- }
-